﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Advanced_Terrain
{
    public class DiscHUD : DrawableGameComponent
    {
        public Boolean visible = false;
        ContentManager _content;
        Model _model;
        Matrix[] _boneTransforms;
        public CustomDisc DiscInfo;
        public Vector3 Position = new Vector3(5,5,0);
        float Scale = .010f;
        public Quaternion Rotation = Quaternion.CreateFromYawPitchRoll(0,MathHelper.ToRadians(-15),0);
        private Texture2D _Texture;
        public Quaternion WiiMoteRotation = Quaternion.Identity;
        public GraphicsDevice graphics;
        public Vector3 CameraPosition = new Vector3();
        public Matrix CameraRotation = new Matrix();
        public float WindPower = 1;
        public DiscHUD(Game game, ContentManager content)
            : base(game)
        {
            _content = content;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }
        protected override void LoadContent()
        {
            _model = _content.Load<Model>("frisbee");
            // Allocate the transform matrix array.
            _boneTransforms = new Matrix[_model.Bones.Count];
            //_Texture = _content.Load<Texture2D>(@"DiscTextures/"+texture);
            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (visible)
            {
                _Texture = _content.Load<Texture2D>(@"DiscTextures/" + DiscInfo.Texture);
                Position = CameraPosition;
                Matrix Temp = Matrix.CreateLookAt(CameraPosition, CameraPosition + new Vector3(0, 0, 1), Vector3.Up);
                Rotation = Quaternion.CreateFromRotationMatrix(CameraRotation)* Quaternion.CreateFromYawPitchRoll(0,MathHelper.ToRadians(90),0);
                if(Game1.MyKeyboard.WiiMoteConnected())
                    Rotation = Quaternion.CreateFromRotationMatrix(CameraRotation) * WiiMoteRotation * Quaternion.CreateFromYawPitchRoll(0, MathHelper.ToRadians(90), 0);
                //Rotation = this.WiiMoteRotation;
                Position += Vector3.Transform(new Vector3(.48f, -.36f, -1.25f), CameraRotation);
                //Position.Z -= 2f; 
            }
                base.Update(gameTime);
            
        }
        public void SetTexture(string TextureName)
        { 
        }
        public override void Draw(GameTime gameTime)
        {
            
            if (visible)
            {
                _model.CopyAbsoluteBoneTransformsTo(_boneTransforms);
                // Draw the model.
                foreach (ModelMesh mesh in _model.Meshes)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.Begin();
                        effect.Projection = CameraProperties.projectionMatrix;
                        effect.World = _boneTransforms[mesh.ParentBone.Index] * Matrix.CreateScale(Scale) * Matrix.CreateFromQuaternion(Rotation) * Matrix.CreateTranslation(Position);
                        //effect.World = effect.World * Matrix.CreateFromQuaternion(Rotation) *Matrix.Identity;
                        effect.View = CameraProperties.View;
                        effect.TextureEnabled = true;
                        effect.Texture = _Texture;
                        effect.EnableDefaultLighting();
                        effect.End();
                    }

                    mesh.Draw();

                }
            }

            base.Draw(gameTime);
        }
}
    
}
